#include "jsoncpp/json/json.h"
#include <iostream>
#include <memory>
/**
 * \brief Parse a raw string into Value object using the CharReaderBuilder
 * class, or the legacy Reader class.
 * Example Usage:
 * $g++ readFromString.cpp -ljsoncpp -std=c++11 -o readFromString
 * $./readFromString
 * colin
 * 20
 */
int main() {
  const std::string rawJson = R"({"Age": 20, "Name": "colin"})";
  const auto rawJsonLength = static_cast<int>(rawJson.length());
  const std::string rawJson1 = R"({"Age": 30, "Name": "colan"})";
  const auto rawJsonLength1 = static_cast<int>(rawJson1.length());

  JSONCPP_STRING err;
  Json::Value root;
  std::cout << sizeof(Json::Value) << std::endl;

  Json::CharReaderBuilder builder;
  const std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
  
  if (!reader->parse(rawJson.c_str(), rawJson.c_str() + rawJsonLength, &root,
                      &err)) {
    std::cout << "error" << std::endl;
    return EXIT_FAILURE;
  }
  const std::string name = root["Name"].asString();
  const int age = root["Age"].asInt();

  std::cout << name << std::endl;
  std::cout << age << std::endl;

  if (!reader->parse(rawJson1.c_str(), rawJson1.c_str() + rawJsonLength1, &root,
                      &err)) {
    std::cout << "error" << std::endl;
    return EXIT_FAILURE;
  }
  const std::string name1 = root["Name"].asString();
  const int age1 = root["Age"].asInt();

  std::cout << name1 << std::endl;
  std::cout << age1 << std::endl;
  return EXIT_SUCCESS;
}
